from lucommon.models import *


# Create your models here.


class JobStatus:
    INIT = 0
    PENDING = 1
    EXECUTING = 2
    SUCCESS = 3
    PART_SUCCESS = 4
    FAIL = 5
    CANCEL = 6


class Job(LuModel):
    status_choices = ((JobStatus.INIT, "初始化中"),
                      (JobStatus.PENDING, "等待执行"),
                      (JobStatus.EXECUTING, "执行中"),
                      (JobStatus.SUCCESS, "成功"),
                      (JobStatus.FAIL, "失败"),)

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=400)
    status = models.IntegerField(choices=status_choices, default=JobStatus.INIT)
    created_at = models.DateTimeField(null=True, auto_now_add=True)
    updated_at = models.DateTimeField(null=True, auto_now=True)

    class Meta:
        db_table = "async_job"


class TaskStatus:
    PENDING = 0
    EXECUTING = 1
    SUCCESS = 2
    FAIL = 3
    CANCEL = 4


class Task(LuModel):
    status_choices = ((TaskStatus.PENDING, "等待执行"),
                      (TaskStatus.EXECUTING, "执行中"),
                      (TaskStatus.SUCCESS, "执行成功"),
                      (TaskStatus.FAIL, "执行失败"),
                      (TaskStatus.CANCEL, "取消"),)

    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=400)
    job_id = models.IntegerField()
    execute_func_name = models.CharField(max_length=400)
    execute_func = models.BinaryField()
    execute_params = models.BinaryField()
    status = models.IntegerField(choices=status_choices, default=TaskStatus.PENDING)
    result = models.JSONField(null=True)
    created_at = models.DateTimeField(null=True, auto_now_add=True)
    updated_at = models.DateTimeField(null=True, auto_now=True)

    def __hash__(self):
        return self.id

    class Meta:
        db_table = "async_task"


class TaskDepend(LuModel):
    id = models.AutoField(primary_key=True)
    task_id = models.IntegerField()
    depend_task_id = models.IntegerField()
    created_at = models.DateTimeField(null=True, auto_now_add=True)
    updated_at = models.DateTimeField(null=True, auto_now=True)

    def __hash__(self):
        return self.id


class Result:
    class Meta:
        db_table = "async_task_result"
